package com.jiantong.user.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.impl.dao.CertificateStationDao;
import com.jiantong.user.model.CertificateStation;
import com.jiantong.user.service.CertificateStationReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by mz on 22/9/17.
 *
 */
@Slf4j
@Service
@RpcProvider
public class CertificateStationReadServiceImpl implements CertificateStationReadService{

    private final CertificateStationDao certificateStationDao;

    @Autowired
    public CertificateStationReadServiceImpl(CertificateStationDao certificateStationDao) {
        this.certificateStationDao = certificateStationDao;
    }

    /**
     * 查询一个市的所有的发证点
     *
     * @param loginUser 当前登录用户
     * @return List<CertificateStation>
     */
    @Override
    public Response<List<CertificateStation>> findByProvinceIdAndCityId(LoginUser loginUser) {
        Integer provinceId = loginUser.getUserProfile().getProvinceId();
        Integer cityId = loginUser.getUserProfile().getCityId();
        Integer regionId = loginUser.getUserProfile().getRegionId();
        Integer streetId = loginUser.getUserProfile().getStreetId();
        try{
            Map<String, Object> criteria = Maps.newHashMap();
            if (provinceId != null && cityId !=null && regionId != null) {
                criteria.put("provinceId", provinceId);
                criteria.put("cityId", cityId);
                criteria.put("regionId",regionId);
            }
            List<CertificateStation> list = certificateStationDao.findByProvinceIdAndCityId(criteria);
            if(list == null){
                log.error("certificateStations(criteria={}) is not found", criteria);
                return Response.fail("certificateStations.not.found");
            }
            return Response.ok(list);
        }catch (Exception e){
            log.error("certificateStations(case={}) is not found", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to query list certificateStations");
        }
    }
}
